package dfferber.xstream;

/*
 * Copyright (C) 2006 Joe Walnes.
 * Copyright (C) 2006, 2007, 2008 XStream Committers.
 * All rights reserved.
 *
 * The software in this package is published under the terms of the BSD
 * style license a copy of which has been included with this distribution in
 * the LICENSE.txt file.
 * 
 * Created on 20. February 2006 by Mauro Talevi
 */

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.ConverterLookup;
import com.thoughtworks.xstream.converters.SingleValueConverter;
import com.thoughtworks.xstream.core.util.FastField;
import com.thoughtworks.xstream.core.util.Fields;
import com.thoughtworks.xstream.mapper.AttributeMapper;
import com.thoughtworks.xstream.mapper.Mapper;
import com.thoughtworks.xstream.mapper.MapperWrapper;

/**
 * Fixes odd behavior from {@link AttributeMapper}
 */
public class BetterAttributeMapper extends AttributeMapper {

	private final Map<String, Class<?>> propertyOfType = new HashMap<String, Class<?>>();
	private final Set<Class<?>> types = new HashSet<Class<?>>();
	private final Set<FastField> propertyFromType = new HashSet<FastField>();
	
	private ConverterLookup converterLookup;

	private Mapper wrapped;
	
	public BetterAttributeMapper(Mapper wrapped, ConverterLookup converterLookup) {
		super(wrapped, converterLookup);
		this.converterLookup = converterLookup;
		this.wrapped = wrapped;
	}

	@Override
	public void addAttributeFor(Class definedIn, String fieldName) {
		propertyFromType.add(new FastField(definedIn, fieldName));
	}

	@Override
	public void addAttributeFor(final String propertyName, final Class type) {
		propertyOfType.put(propertyName, type);
	}

	@Override
	public void addAttributeFor(final Class type) {
		types.add(type);
	}

	private SingleValueConverter getLocalConverterFromItemType(Class type) {
		Converter converter = converterLookup.lookupConverterForType(type);
		if (converter != null && converter instanceof SingleValueConverter) {
			return (SingleValueConverter) converter;
		}
		return null;

	}

	public SingleValueConverter getConverterFromItemType(String fieldName, Class type, Class definedIn) {
		if (shouldLookForSingleValueConverter(fieldName, type, definedIn)) {
			SingleValueConverter converter = getLocalConverterFromItemType(type);
			if (converter != null) {
				return converter;
			}
		}
		return wrapped.getConverterFromItemType(fieldName, type, definedIn);
	}

	public boolean shouldLookForSingleValueConverter(String fieldName, Class type, Class definedIn) {
		FastField field = new FastField(definedIn, fieldName);
		return propertyFromType.contains(field) || propertyOfType.get(fieldName) == type 	|| types.contains(type);
	}

	public SingleValueConverter getConverterFromAttribute(Class definedIn, String attribute, Class type) {
		if (shouldLookForSingleValueConverter(attribute, type, definedIn)) {
			SingleValueConverter converter = getLocalConverterFromItemType(type);
			if (converter != null) {
				return converter;
			}
		}
		return wrapped.getConverterFromAttribute(definedIn, attribute, type);
	}
}
